// @ts-ignore
import * as iot from 'alibabacloud-iot-device-sdk'
import log from './logger'
import {AliyunIot} from "./interfaces";
import settings from "./settings";
import led from "./led";

class AliyunIotImpl implements AliyunIot {
    device: any

    ledService(res: any, reply: any) {
        log.info('led called,res:' + JSON.stringify(res));
        led.send(res.params.text1, res.params.text2, res.params.text3, res.params.hostname)
        const result = {
            data: {},
            code: 200
        }
        reply(result);
    }

    init() {
        this.device = iot.device({
            productKey: settings.productKey,
            deviceName: settings.deviceName,
            deviceSecret: settings.deviceSecret
        });
        this.device.on('connect', () => {
            log.info('connect successfully!');
        });

        this.device.onService('led', this.ledService.bind(this));
    }
}

const aliyunIot = new AliyunIotImpl();
export default aliyunIot;